package com.sskj.givecoin.v.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.givecoin.R;
import com.sskj.givecoin.adapter.slimadapter.IViewInjector;
import com.sskj.givecoin.adapter.slimadapter.SlimAdapter;
import com.sskj.givecoin.adapter.slimadapter.SlimInjector;
import com.sskj.givecoin.common.widget.ToolBarLayout;
import com.sskj.givecoin.m.bean.CommissionBean;
import com.sskj.givecoin.mvchelper.ModelRx2DataSource;
import com.sskj.givecoin.p.activity.CommissionDetailsPresenter;
import com.sskj.givecoin.utils.ClickUtil;
import com.sskj.givecoin.utils.NumberUtil;
import com.sskj.givecoin.utils.TimeFormatUtil;
import com.sskj.givecoin.v.base.BaseActivity;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

/**
 * Created by lv on 18-6-7.
 * 佣金明细界面
 */

public class CommissionDetailActivity extends BaseActivity<CommissionDetailsPresenter> {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R.id.tv_filter)
    TextView tvFilter;
    @BindView(R.id.ll_time)
    LinearLayout llTime;
    @BindView(R.id.commission_toolbar)
    ToolBarLayout commissionToolbar;
    private SlimAdapter slimAdapter;
    private boolean isStart;
    private TimePickerView startPicker;
    private MVCCoolHelper<List<CommissionBean>> mvcCoolHelper;
    private View popupView;
    private TextView tvStartTime;
    private TextView tvEndTime;
    private PopupWindow popupWindow;

    @Override
    protected void initView() {
        commissionToolbar.setLeftButtonOnClickLinster(v -> finish());
        ClickUtil.click(300, tvFilter, this::showTimePop);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapter = SlimAdapter.create().register(R.layout.item_commission, new SlimInjector<CommissionBean>() {
            @Override
            public void onInject(CommissionBean data, IViewInjector injector) {
                injector.text(R.id.tv_title, data.getUsername_down() +"\n"+data.getMobile())
                        .text(R.id.tv_fee, NumberUtil.keepNoZore(data.getFee()))
                        .text(R.id.tv_money, NumberUtil.keepNoZore(data.getFee_yongjin()))
                        .text(R.id.tv_time, data.getAdd_time());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<CommissionBean> dataSource =
                new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<CommissionBean>)
                        page -> mPresenter.loadData(page, tvStartTime == null ? "" : TimeFormatUtil.parseLongB(tvStartTime.getText().toString()), tvEndTime == null ? "" : TimeFormatUtil.parseLongB(tvEndTime.getText().toString())), 15);
        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcCoolHelper.setDataSource(dataSource);
        mvcCoolHelper.setAdapter(slimAdapter);
        mvcCoolHelper.refresh();
    }

    /**
     * 显示时间选择弹窗
     */
    private void showTimePop() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        } else {
            popupView = LayoutInflater.from(this).inflate(R.layout.popup_fabi_time_select, null);
            tvStartTime = popupView.findViewById(R.id.tv_start_time);
            tvEndTime = popupView.findViewById(R.id.tv_end_time);
            View view = popupView.findViewById(R.id.view);
            ClickUtil.click(popupView.findViewById(R.id.tv_apply), () -> {
                popupWindow.dismiss();
                mvcCoolHelper.refresh();
            });
            ClickUtil.click(popupView.findViewById(R.id.tv_cancel), () -> {
                tvStartTime.setText("");
                tvEndTime.setText("");
            });
            initTime();
            ClickUtil.click(view, () -> {
                popupWindow.dismiss();
            });
            popupWindow = new PopupWindow(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            popupWindow.setContentView(popupView);
            popupWindow.setBackgroundDrawable(new ColorDrawable(0x66000000));
//        popupWindow.setOutsideTouchable(true);
//        popupWindow.setAnimationStyle(R.style.AnimDown);
//            popupWindow.setClippingEnabled(true);

            if (Build.VERSION.SDK_INT > 24) {
                Rect rect = new Rect();
                llTime.getGlobalVisibleRect(rect);
                int h = llTime.getResources().getDisplayMetrics().heightPixels - rect.bottom;
                popupWindow.setHeight(h);
            }
            popupWindow.showAsDropDown(llTime);
//            popupWindow.showAsDropDown(llTime);
        }


    }

    private void initTime() {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DAY_OF_YEAR, -7);
        tvStartTime.setText(TimeFormatUtil.SF_FORMAT_B.format(instance.getTime()));
        tvEndTime.setText(TimeFormatUtil.SF_FORMAT_B.format(Calendar.getInstance().getTime()));
        ClickUtil.click(tvStartTime, () -> showTime(true));
        ClickUtil.click(tvEndTime, () -> showTime(false));
    }

    private void showTime(boolean isStart) {
        this.isStart = isStart;

        startPicker = new TimePickerBuilder(this, (date, v) -> {
            if (this.isStart) {
                tvStartTime.setText(TimeFormatUtil.limitMaxDate(date));
            } else {
                tvEndTime.setText(TimeFormatUtil.limitMaxDate(date));
            }
        })
                .setDecorView((ViewGroup) popupView).build();
        try {
            Date parse = TimeFormatUtil.SF_FORMAT_B.parse(isStart ? tvStartTime.getText().toString() : tvEndTime.getText().toString());
            Calendar instance = Calendar.getInstance();
            instance.setTime(parse);
            startPicker.setDate(instance);
            startPicker.show();
        } catch (ParseException e) {
            startPicker.show();

        }
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, CommissionDetailActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_commission_detail;
    }

    @Override
    public CommissionDetailsPresenter getPresenter() {
        return new CommissionDetailsPresenter();
    }


}
